
// #ifndef VUE3
import Vue from 'vue'
import App from './App'
import store from './store'
Vue.config.productionTip = false

App.mpType = 'app'

const app = new Vue({
    ...App
})
app.$mount()
// #endif

// #ifdef VUE3
import { createSSRApp } from 'vue'
import App from './App.vue'
import store from './store'
import Request from 'luch-request';
export function createApp() {
  const app = createSSRApp(App)
  app.use(store)
  
  
  
  console.log(app.$store,"app.$store---",store)

  
	//公共JS
  let appStore = store;
  let cTop = uni.getSystemInfoSync().statusBarHeight + 44;
  let SystemInfoSync = {cTop};
  let menuButtonInfo = {};
  // #ifndef H5
  menuButtonInfo = uni.getMenuButtonBoundingClientRect()
  SystemInfoSync = {...SystemInfoSync,...menuButtonInfo}
  // #endif
  // #ifdef H5
  SystemInfoSync = {
	  bottom: 56,
	  cTop: 44,
	  height: 32,
	  left: 226,
	  right: 313,
	  top: 24,
	  width: 87,
  }
  // #endif
  
  
  
  const http = new Request({
    baseURL: apiUrl + apiPath,
    timeout: 8000,
    method: 'GET',
    header: {
      Accept: 'text/json',
      'Content-Type': 'application/json;charset=UTF-8',
      platform: "WechatMiniProgram",
    },
    // #ifdef APP-PLUS
    sslVerify: false,
    // #endif
    // #ifdef H5
    // 跨域请求时是否携带凭证（cookies）仅H5支持（HBuilderX 2.6.15+）
    withCredentials: false,
    // #endif
    
  });
  console.log(http.interceptors.response,"http.interceptors.responsev--",http.interceptors.response.use)
  http.interceptors.response.use(
  (response) => {
  	console.log(response,"response------------73")
	return Promise.resolve(response.data);
  },(error) => {
  	console.log(error)
  })
  const request = (config) => {
    if (config.url[0] !== '/') {
      config.url = apiUrl + apiPath + config.url;
    }
    return http.middleware(config);
  };
	
  const appMsg = (title, duration = 1500, mask = false, icon = 'none') => {
  	//统一提示方便全局修改
  	title = title || "请输入完整信息";
  	uni.showToast({
  		title,
  		duration,
  		mask,
  		icon
  	});
  }
  app.config.globalProperties.$msg = appMsg;
  
  appStore.commit('setJiaonang',SystemInfoSync);
  let apiUrl = uni.getStorageSync("baseUrl") || appStore.state.baseUrl;
  let apiPath = appStore.state.apiPath;
  app.config.globalProperties.$queryApi = function(method,url, data, callsuc, token,err) {
  	method = method || "post";
  	token = token == "false" ? false : true;
  	data = data ? data : {};
  	let userInfo = appStore.state.userInfo; 
  	uni.showLoading();
  	let userInfoId = ""
  	 try{
  	 	userInfoId = appStore.state.userInfo || {};
  	 }catch(e){
  	 	//TODO handle the exception
  	 }
  	if (!data.token && token && userInfoId && userInfoId.token) {
  		data.token = userInfoId.token;
  	}

	request({
	  url: url,
	  method: method,
	  data: data,
	  success:(res) => {
  			
  			console.log(res)
		}
	})
  	// uni.request({
  	// 	url: apiUrl + apiPath + url,
  	// 	method: 'post',
  	// 	data: data,
  	// 	header: {
			// Accept: 'text/json',
			// 'Content-Type': 'application/json;charset=UTF-8',
  	// 		// 'content-type': 'application/x-www-form-urlencoded'
			// token: 'caf67c49-d40f-4d69-b71e-4511d194a0de'
  	// 	},
  	// 	success: (res) => {
  	// 		uni.hideLoading();
  	// 		console.log(res)
  	// 		err instanceof Function && err(res.data);
  	// 		if (res.data.code == 1) {
  	// 			callsuc instanceof Function && callsuc(res.data);
  	// 			if(res.data.msg){
  	// 				appMsg(res.data.msg)
  	// 			}
  	// 		} else
  	// 		if (res.data.code == 0) {
  	// 			try{
  	// 				uni.hideLoading();
  	// 			}catch(e){
  	// 				//TODO handle the exception
  	// 			}
  	// 			if(res.data.msg.indexOf("已领取过该优惠券") != -1){
  	// 				uni.setStorageSync('lingquQ',1)
  	// 			}
  	// 			if(res.data.msg.indexOf("token is error") !=  -1 ){
  	// 				uni.reLaunch({
  	// 					url:"/pages/login/login"
  	// 				})
  	// 			}else{
  	// 				appMsg(res.data.msg)
  	// 			}
  	// 			return false
  	// 		} else
  	// 		if (res.data.status == 401) {
  	// 			appMsg("请重新登录")
  	// 			// #ifndef MP
  	// 				appStore.commit("logout");
  	// 				uni.reLaunch({
  	// 					url:"/pages/login/login"
  	// 				})
  	// 			// #endif
  	// 			// #ifdef MP
  	// 				appStore.commit("logout");
  	// 				setTimeout(()=>{
  	// 					uni.switchTab({
  	// 						url: '../../pages/home/home'
  	// 					})
  	// 				},1000)
  	// 			// #endif
  	// 		} 
  	// 	},
  	// 	fail: (err) => {
  	// 		appMsg("网络请求出错")
  			
  	// 		setTimeout(()=>{
  	// 			uni.hideLoading();
  	// 			uni.reLaunch({
  	// 				url:"/pages/login/noData"
  	// 			})
  	// 		},4000)
  			
  			
  	// 	}
  	// });
  }
  app.config.globalProperties.$resetUserInfo = function(back) {
  	app.$postApi("App/User/userInfo",{},res1=>{
  		let userinfo = {
  			...res1.data,
  			token:appStore.state.userInfo.id
  		}
  		console.log(res1)
  		appStore.commit("login",userinfo)
  		back instanceof Function  && back()
  	})
  }
  app.config.globalProperties.$getApiTime = function(url, data, callsuc, token) {
  	token = token == "false" ? false : true;
  	data = data ? data : {};
  	console.log("token", token);
  	let userInfo = uni.getStorageSync('userInfo');
  	if (token && userInfo) {
  		data.token = userInfo.remember_token;
  		data.page = 1;
  		data.paginate = 200;
  	}
  	uni.request({
  		url: apiUrl + url,
  		method: 'post',
  		data: data,
  		header: {
  			'content-type': 'application/x-www-form-urlencoded'
  		},
  		success: (res) => {
  			console.log(res)
  			if (res.data.status == 200 || res.data.status == 202) {
  				// appMsg(res.data.msg)
  				callsuc instanceof Function && callsuc(res.data)
  			} else
  			if (res.data.status == 401) {
  				appMsg("登录已过期,请重新登录")
  				// #ifndef MP
  					appStore.commit("logout");
  					uni.reLaunch({
  						url: '../login/yLogin',
  						success(){
  							// location.reload()
  						}
  					})
  				// #endif
  				// #ifdef MP
  					appStore.commit("logout");
  					callsuc instanceof Function && callsuc(res.data)
  					
  				// #endif
  			}
  		},
  		fail: (err) => {
  			appMsg("网络错误,请联系管理员")
  		}
  	});
  }
  

  
  app.config.globalProperties.$getDate = (myDate,type,timec) => {
  	if(!myDate){
  		myDate = new Date()
  	}else{
  		if(timec == "c"){
  			myDate = new Date(parseInt(myDate) * 1000)
  		}else{
  			myDate = new Date(myDate)
  		}
  	}
  	let year = myDate.getFullYear();   //获取系统的年；
  	let month = myDate.getMonth()+1;   //获取系统月份，由于月份是从0开始计算，所以要加1
  	
  	let date = myDate.getDate(); // 获取系统日，
  	
  	let hours = myDate.getHours(); //获取系统时，
  	let min = myDate.getMinutes(); //分
  	let sec = myDate.getSeconds(); //秒
  	if(month<10){ month = "0"+month}
  	if(date<10){ date = "0"+date }
  	if(hours<10){ hours = "0"+hours}
  	if(min<10){ min = "0"+min}
  	if(sec<10){ sec = "0"+sec }
  	
  	if(type == '月-号'){
  		return month+'月'+date+'号'
  	}else
  	if(type == '年-月-日'){
  		return year+'年'+month+'月'+date+'日'
  	}else
  	if(type == 's-s-s'){
  		return year+'-'+month+'-'+date
  	}else
  	if(type == 's:s:s'){
  		return hours+':'+min+':'+sec
  	}else
  	if(type == 's-s-s s:s:s'){
  		return year+'-'+month+'-'+date+" "+hours+':'+min+':'+sec
  	}else{
  		return year+'-'+month+'-'+date+" "+hours+':'+min+':'+sec
  	}
  }
  
  app.config.globalProperties.$calcPhone = function(el){
  	if(el){
  		return el.substr(0,3) + "****" + el.substr(7)
  	}else{
  		return "未绑定手机号"
  	}
  }
  
  app.config.globalProperties.$qianweifu = function(num){
  	num=num.toString().split(".");  // 分隔小数点
  	    var arr=num[0].split("").reverse();  // 转换成字符数组并且倒序排列
  	    var res=[];
  	    for(var i=0,len=arr.length;i<len;i++){
  	      if(i%3===0&&i!==0){
  	         res.push(",");   // 添加分隔符
  	      }
  	      res.push(arr[i]);
  	    }
  	    res.reverse(); // 再次倒序成为正确的顺序
  	    if(num[1]){  // 如果有小数的话添加小数部分
  	      res=res.join("").concat("."+num[1]);
  	    }else{
  	      res=res.join("");
  	    }
  	    return res;
  }
  //公共JS end
  
  return {
    app
  }
}

// #endif